package org.ns.summer.mvc.mappinghandler;

import org.ns.summer.core.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.function.Supplier;

public class HandlerMethod {

    private Method method;
    private String beanName; // 控制器实例名
    private Supplier<?> supplier;
    private Object object;

    public HandlerMethod(Method method, String beanName, Supplier<?> supplier) {
        this.method = method;
        this.beanName = beanName;
        this.supplier = supplier;
    }

    public Object getObject() {
        if (object == null) {
            synchronized (this) {
                object = supplier.get();
            }
        }
        return object;
    }

    public Method getMethod() {
        return method;
    }
    public String getBeanName() {
        return beanName;
    }

    public <T extends Annotation> T getAnnotation(Class<?> annotationType) {
        return (T)AnnotationUtils.getAnnotation(method, annotationType);
    }
    public boolean existAnnotation(Class<?> annotationType) {
        return AnnotationUtils.existAnnotation(method, annotationType);
    }
}
